<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import= "com.roadmap.struts.action.ActionConst"%>
<%@ page import="com.roadmap.common.RdcResourceBundle"%>
<%@ page import="com.roadmap.common.BaseConst"%>
<%@ page import="com.roadmap.ui.HTMLComponentWriter"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String user_ssn_id = (String)request.getSession().getAttribute(ActionConst.Aspirant_Id_In_Session);
String user_req_id = (String)request.getParameter(ActionConst.Aspirant_Id_In_Request);
if(user_req_id == null){
	user_req_id = user_ssn_id;
}
String task_req_id = (String)request.getParameter(ActionConst.Item_Id_In_Request);
if(task_req_id == null){
	task_req_id = "";
}
String act_type = (String)request.getParameter(ActionConst.Method_Symbol);
if(act_type == null) {
	act_type = "view";
}

HTMLComponentWriter htmlCom = new HTMLComponentWriter(out);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/> 
<link href="res/css/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/life.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/widget.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/RdcAjax.js"></script>
<script type="text/javascript" src="dwr/interface/TaskHandler.js"></script>
<script type="text/javascript" src="res/js/util.js"></script>
<script type="text/javascript" src="res/js/func.js"></script>
<script type="text/javascript" src="res/js/widget.js"></script>
<script type="text/javascript" src="res/js/component.js"></script>
<title> 任务 | <%=RdcResourceBundle.COMPANY_NAME %></title>
</head>

<body>
<div id="container">
	<jsp:include page="/admin/myheader.jsp"></jsp:include>
	
	<!-- main body -->
	<div class="mainbody"><div class="manager">
	<jsp:include page="/admin/life/nav_btns.jsp"/>
			
		<!-- middle part: content -->
		<div class="middle-part">
			<div class="section" style="border-bottom: 1px solid #E78F28">
				<div class="title-sp">
					<a href="aspirant.do?uid=<%=user_req_id %>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>"/></a>
					<span class="doubleGreat">&gt;&gt;</span>
					
					<logic:present name="task">
						<logic:notEmpty name='task' property='goalId'>											
							<a href="goal.do?act=view&uid=<%=user_req_id %>&id=<bean:write name='task' property='goalId'/>">
								<span id="viewTaskGoal">目标:&nbsp;<bean:write name="task" property="goalName"/></span>
							</a>
						</logic:notEmpty>
						
						<logic:empty name='task' property='goalId'>
							<a href="task.do?act=all&uid=<%=user_req_id %>">所有任务</a>
						</logic:empty>
						
						<span class="doubleGreat">&gt;&gt;</span>
						<bean:write name="task" property="whatItIs"/>
					</logic:present>
					<logic:notPresent name="task">
						<a href="task.do?act=all&uid=<%=user_req_id %>">所有任务</a>
					</logic:notPresent>
				</div>
					
				<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
					<div id="btnSubSec" class="float-right"><input class="dropParentBtn" type="button" value="更多操作" onclick="dropMenu.showWith(this);"/></div>
				</logic:present>
			</div>
			
			<% if(act_type.equals("create")) {%>
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<!-- following part can only be viewed by the item owner -->
			<div id="createTask" class="section">
				<div id="loadGoalsDiv" class="sub-section" style="display:none;">
					<table class="life-table">
						<tr><td colspan="5"><span class="prompt-message">您可以选择一个目标，为该目标创建一个任务，也可以不用选择目标而直接创建任务</span></td></tr>
						<logic:notPresent name="goals">
						<tr><td colspan="5"><span class="prompt-message">没有可执行目标，点击<a href='goal.do?act=create'>这里创建</a></span></td></tr>
						</logic:notPresent>
						
						<logic:present name="goals">
						<tr><th></th><th>目标名称</th><th>预定开始日期</th><th>预定结束日期</th><th>已分配</th></tr>
						<logic:iterate id="goal" name="goals">
						<logic:notEmpty name="goal" property="id">
						<bean:define id="gid" name="goal" property="id"/>
						<tr id="goaltr<%=gid%>">
							<td height="22px;"><input id="box<%=gid %>" name="box" type="checkbox" onclick="chooseOneBox(this); selectedGoalId=(this.checked)?'<%=gid%>':null;"/></td>
							<td align="left"><a href="goal.do?act=view&uid=<%=user_ssn_id %>&id=<%=gid%>"><span id="name<%=gid %>"><bean:write name="goal" property="name"/></span></a></td>
							<td><span id="sst<%=gid %>"><bean:write format="yyyy-MM-dd" name="goal" property="scheduledStartDate"/></span></td>
							<td><span id="sed<%=gid %>"><bean:write format="yyyy-MM-dd" name="goal" property="scheduledEndDate"/></span></td>
							<td><span id="pro<%=gid %>"><bean:write name="goal" property="assignedProportion"/></span>%</td>
						</tr>
						</logic:notEmpty>
						</logic:iterate>
						</logic:present>
						
						<tr><td colspan="4">
							<input id="minStartDate" type="hidden" value="0"/>
							<input id="maxEndDate" type="hidden" value="0"/>
							<input id="maxProportion" type="hidden" value="100"/>
							<input type="button" class="confirmBtn" value="下一步" onclick="nextStep();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="goToTasksView();"/>
						</td></tr>
					</table>
				</div>
				
				<div id="subCreateTaskDiv" class="sub-section" style="display: none">
				<table class="life-table">
					<tr>
						<td class="row-td">所属目标</td>
						<td class="input-td" colspan="3"><span id="selectedGoal"></span></td>
					</tr>
					<tr>
						<td class="row-td">任务描述</td>
						<td class="input-td" colspan="3"><textarea id="newContent" class="input-text" style="width:402px;height:100px;"
																						title="任务描述(1~500)"></textarea></td>
					</tr>
					<tr>
						<td class="row-td">预定执行日期</td>
						<td class="input-td" colspan="3">
							<input id="newStartDate" type="text" class="shortDate" maxlength="10" title="预定开始日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/>
							<input id="newStartTime" type="text" class="shortTime" maxlength="5" title="预定开始时间(HH:mm)" onfocus="timePicker.showWith(this);"/>
							<span>到</span>
							<input id="newEndDate" type="text" class="shortDate" maxlength="10" title="预定结束日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/>
							<input id="newEndTime" type="text" class="shortTime" maxlength="5" title="预定结束时间(HH:mm)" onfocus="timePicker.showWith(this);"/>
						</td>
					</tr>
					<tr>
						<td class="row-td">占目标比重</td><td class="input-td"><input id="newProportion" class="hundred" maxlength="3" title="占目标比重(1~100)"/>%</td>
						<td class="row-td" style="text-align:right;">等级</td>
						<td class="input-td">
							<select id="newTaskRank" class="select">
								<%
									for(int i = 0; i < RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS.length; i++){
										String selected = (i == 0) ? "selected" : null;
										out.write(String.format("<option %s>%s</option>", selected, RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS[i]));
									}
								%> 
							</select>
						</td>
					</tr>
					<jsp:include page="/pagelet/accessEditPagelet.jsp"/>
					<tr>
						<td colspan="4">
							<input type="button" class="confirmBtn" value="上一步" onclick="lastStep();"/>
							<input id="createTask" type="button" class="confirmBtn" value="保存" onclick="createNewTask();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="goToTasksView();"/>
						</td>
					</tr>
				</table>
				</div>
			</div>
			</logic:present>
			<%} if(act_type.equals("view")) {%>
			<logic:notPresent name="task">
				<div class="section"><span class="info-message">您查看的任务不存在，可能已被删除，或者您输入的URL有误</span></div>
			</logic:notPresent>
			
			<logic:present name="task">
			<div id="viewTask" class="section" style="display: none"><div class="sub-section">
				<bean:define id="status" name="task" property="status"/>
				<bean:define id="rank" name='task' property='taskRank'></bean:define>
				<table class="life-table">
					<tr>
						<td class="row-td">任务描述</td>
						<td colspan="3" class="input-td"><span id="viewContent" class="desc">
							<bean:write filter="false" name="task" property="content"/></span>
							<input id="goalIdFromAction" type="hidden" value="<bean:write name='task' property='goalId'/>"/>
						</td>
					</tr>
					<tr>
						<td class="row-td">当前状态</td>
						<td class="input-td">
						   <span id="viewStatusDes" ><%=RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[Integer.valueOf(status.toString())] %></span>
						   <input id="viewStatus" type="hidden" value="<%=status %>"/>
						</td>								
						<td class="row-td">自评得分</td><td class="input-td"><span id="viewScore"><bean:write name="task" property="selfScore"/></span></td>	
					</tr>					
					<tr>
						<td class="row-td">占目标比重</td><td class="input-td"><span id="viewProportion"><bean:write name="task" property="proportionOfGoal"/></span>%</td>	
						<td class="row-td">实际进度</td><td class="input-td"><span id="viewProgress"><bean:write name="task" property="progress"/>%</span></td>	
					</tr>
					<tr>
						<td class="row-td">预定开始时间</td><td class="input-td"><span id="viewStartDate"><bean:write format="yyyy-MM-dd HH:mm" name="task" property="scheduledStartDate"/></span></td>	
						<td class="row-td">预定结束时间</td><td class="input-td"><span id="viewEndDate"><bean:write format="yyyy-MM-dd HH:mm" name="task" property="scheduledEndDate"/></span></td>	
					</tr>
					<tr>
						<td class="row-td">实际开始日期</td><td class="input-td" width="240px"><span id="realStartDate"><bean:write format="yyyy-MM-dd" name="task" property="realStartDate"/></span></td>
						<td class="row-td">实际结束日期</td><td class="input-td"><span id="realEndDate"><bean:write format="yyyy-MM-dd" name="task" property="realEndDate"/></span></td>						
					</tr>
					<tr>
						<td class="row-td">任务等级</td><td class="input-td"><span id="viewRank"><%=RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS[((Integer)rank).intValue()] %></span></td>
						<td class="row-td">执行记录</td><td class="input-td">
							<logic:notEmpty name="task" property="latestExecDate">最新<bean:write format="yyyy-MM-dd" name="task" property="latestExecDate"/>,</logic:notEmpty> 
							<a href="exec.do?act=all&uid=<%=user_req_id %>&pid=<%=task_req_id %>" title="查看任务的所有执行记录">共<bean:write name="task" property="execCount"/>条</a>&nbsp;<span class="doubleGreat">&gt;&gt;</span>
						</td>
					</tr>								
					<tr>
						<td class="row-td">创建时间</td><td class="input-td"><span id="viewCreated"><bean:write format="yyyy-MM-dd HH:mm:ss" name="task" property="createdTime"/></span></td>
						<td class="row-td">上次更新时间</td><td class="input-td"  width="150px"><span id="viewUpdated"><bean:write format="yyyy-MM-dd HH:mm:ss" name="task" property="updatedTime"/></span></td>
					</tr>
					
					<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
					<tr>
						<td class="row-td">访问权限</td><td class="input-td" colspan="3"><span id='viewVisibility'><bean:write name='task' property='visibilityDesc'/></span></td>
					</tr>
					<tr>
						<td colspan="4"><input type="button" class="confirmBtn" value="编辑" onclick="switchToEditSec();RDC.gui.hide('viewTask');RDC.gui.display('editTask');"/>
							<input type="button" class="cancelBtn" value="关闭" onclick="goToTasksView();"/>
						</td>
					</tr>
					</logic:present>
				</table>
			</div></div>
			
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<div id="editTask" class="section" style="display: none"><div class="sub-section">
				<table class="life-table">
					<tr>
						<td class="row-td">任务描述</td>
						<td class="input-td" colspan="3"><textarea id="editContent" class="input-text" style="width:400px;height:100px;"
															title="任务描述(1~500)"></textarea></td>
					</tr>
					<tr>
						<td class="row-td">预定执行日期</td>
						<td class="input-td" colspan="3">
							<input id="editStartDate" type="text" class="shortDate" maxlength="10" title="预定开始日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/>
							<input id="editStartTime" type="text" class="shortTime" maxlength="5" title="预定开始时间(HH:mm)" onfocus="timePicker.showWith(this);"/>
							<span>到</span>
							<input id="editEndDate" type="text" class="shortDate" maxlength="10" title="预定结束日期(yyyy-MM-dd)" onfocus="datePicker.showWith(this);"/>
							<input id="editEndTime" type="text" class="shortTime" maxlength="5" title="预定结束时间(HH:mm)" onfocus="timePicker.showWith(this);"/>
						</td>
					</tr>
					<tr>
						<td class="row-td">占目标比重</td><td class="input-td"><input id="editProportion" class="hundred" maxlength="3" title="占目标比重(1~100)"/>%</td>
						<td class="row-td">等级</td>
						<td class="input-td">
							<select id="editTaskRank" class="select">
								<%
									for(int i = 0; i < RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS.length; i++){
										out.println("<option value='" + RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS[i] +"'>");
										out.println(RdcResourceBundle.LIFETASK_RNAK_DESCRIPTIONS[i]);
										out.println("</option>");
									}
								%> 
							</select>
						</td>
					</tr>
					<tr>
						<td class="row-td">自评得分</td><td class="input-td"><input id="editScore" class="hundred" maxlength="3" type="text" title="自评分(1~100)"/></td>
						<td class="row-td">访问权限</td><td class="input-td"><% htmlCom.writeVisibilitySelect(request); %></td>
					</tr>
					<tr>
						<td colspan="4">							
							<input id="saveTask" type="button" class="confirmBtn" value="保存" onclick="saveTask();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="viewTaskFromEdit();"/>
						</td>
					</tr>					
				</table>
			</div></div>
			</logic:present>
			
			<div id="taskExecSec" class="section" style="display: none"><div style="float:left;">
				<logic:present name="task">
				<logic:notEmpty name='task' property='goalId'>
					<span class="doubleGreat">&gt;&gt;</span>
					<a href="task.do?act=all&uid=<%=user_req_id %>&pid=<bean:write name='task' property='goalId'/>">
						<span id="viewTaskGoal">同属目标的其他任务</span>
					</a><br/><br/>
				</logic:notEmpty>
				</logic:present>
				<span class="doubleGreat">&gt;&gt;</span>&nbsp;<a href="exec.do?act=all&uid=<%=user_req_id %>&pid=<%=task_req_id %>">任务的所有执行记录</a>
			</div></div>
			
			<!-- show comments -->
			<jsp:include page="/pagelet/commentPagelet.jsp"/>
			</logic:present>
			<%} %>
	</div>
	
	<!-- right part: -->
	<div class="right-part">
	</div>
	
	</div>
	
	<div class="clear-div"></div>
	</div>
	
	<jsp:include page="/res/jsp/footer.jsp"></jsp:include>
</div>


<script type="text/javascript">
	var sections = ["create", "view", "edit"];
	var taskId = "<%=task_req_id%>"; // which task is chosen
	var req_uid = "<%=user_req_id%>";
	var ssn_uid = "<%=user_ssn_id%>";
	var actType= "<%=act_type%>";
	var selectedGoalId = null;	
	var dropMenu = null;
	var commentUtil = null;
	
	window.onload = function(){
		showSection(actType);
		if(actType != "create"){
			commentUtil = new CommentUtil("LifeTask", taskId, ssn_uid, req_uid);
			if(typeof createDropMenu != "undefined") {
				createDropMenu();
			}
		}	
	}
		
	function showSection(sec){
		//RDC.gui.hideMsg("taskMsg");
		var isValid = false;
		for(var i = 0; i < sections.length; i++){
			if(sec != sections[i]){
				RDC.gui.hide(sections[i] + "Task");
			}
			else {
				RDC.gui.display(sections[i] + "Task");
				isValid = true;
								
				if(sections[i] == "view"){
				<% 
					Object task = request.getAttribute("task");
					if(task != null){
				%>
					selectedGoalId = ($("goalIdFromAction").value != "") ? $("goalIdFromAction").value : null;
					RDC.gui.display("taskExecSec");
					RDC.gui.display("btnSubSec");
				<%} else{%>	
					$((sections[i] + "Task")).innerHTML = "<div class='prompt-message'>您要查看的任务不存在，可能已被删除</div>";
				<%}%>
				}
				else if(sections[i] == "create") {
					<%
						String pid = request.getParameter(ActionConst.Parent_Id_In_Request);
						if(pid != null) { 
					%>
							var boxes = document.getElementsByName("box");
							var start = "box".length;
							if(boxes != null){
								for(i = 0; i < boxes.length; i+=1){
									if(boxes[i].id.substring(start, boxes[i].id.length) == "<%=pid %>"){
										boxes[i].checked = true;
										break;
									}
								}
								
								if(i == boxes.length){
									PromptDialog.show("不能创建任务, 可能原因是：<br>1.指定的目标不存在<br>2.该目标已经完成或者被取消");
								}
								else {
									nextStep();
								}
							}
					<%	} else {%>
							RDC.gui.display("loadGoalsDiv");
					<%}%>
					RDC.gui.hide("btnSubSec");
				}							
			}
		}
		
		if(!isValid){
			PromptDialog.show("不合法的URL参数");
		}
	}
</script>

<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
<script type="text/javascript">	
	var datePicker = new DatePicker();
	var timePicker = new TimePicker();
		
	function findCheckedBox(){
		var boxes = document.getElementsByName("box");
		var start = "box".length;
		if(boxes != null){
			for(i = 0; i < boxes.length; i+=1){	
				if(boxes[i].checked){
					selectedGoalId = boxes[i].id.substring(start, boxes[i].id.length);
					break;
				}
			}
		}
	}
	
	function goToTasksView(){
		var uid = (req_uid == "" || req_uid == null) ? ssn_uid : req_uid;
		window.location.href = "task.do?act=all&uid=" + uid;
	}
	
	function setCurTask(tid){
		taskId = tid;
	}
	
	function getCurTask(){
		return taskId;
	}

	function chooseOneBox(box){
		var boxes = document.getElementsByName("box");
		if(boxes != null){
			for(i = 0; i < boxes.length; i+=1){	
				boxes[i].checked = (boxes[i] == box) ? box.checked : false;
			}
		}
	}
	
	// -------------------------------------------
	// create a task
	function lastStep(){
		RDC.gui.display("loadGoalsDiv");
		RDC.gui.hide("subCreateTaskDiv");
	}
	
	function nextStep(){
		RDC.gui.hide("loadGoalsDiv");
		RDC.gui.display("subCreateTaskDiv");
		
		findCheckedBox();
		
		var dt = new Date();
		if(selectedGoalId != null){
			$("minStartDate").value = $("sst" + selectedGoalId).innerHTML;
			$("maxEndDate").value = $("sed" + selectedGoalId).innerHTML;
			$("maxProportion").value = 100 - parseInt($("pro" + selectedGoalId).innerHTML);
			$("selectedGoal").innerHTML = "<a href=goal.do?act=view&uid=" + req_uid + "&id=" + selectedGoalId + ">"
											+  $("name" + selectedGoalId).innerHTML + "</a>";
			$("newProportion").disabled = false;
			$("newProportion").value = $("maxProportion").value;
		}
		else {			
			$("minStartDate").value = -1;
			$("maxEndDate").value = -1;
			$("maxProportion").value = 100;
			$("selectedGoal").innerHTML = "未选择";
			$("newProportion").disabled = true;
			$("newProportion").value = 100;
		}
		$("newContent").value = "";			
		$("newStartDate").value = $("newEndDate").value = RDC.util.dateToString(dt, "yyyy-MM-dd");
		$("newStartTime").value = RDC.util.dateToString(dt, "HH:mm");
		dt.setHours(dt.getHours() + 1);
		$("newEndTime").value = RDC.util.dateToString(dt, "HH:mm");		
		$("newContent").focus();
		$("newProportion").title = "占目标比重(1~" + $("newProportion").value + ")";
		setCurTask(null);
	}
	
	function checkDateFormat(value, format, msg){
		if(RDC.util.isValidDate(value, format)){
			return true;
		}
		else {
			PromptDialog.show(msg);
			return false;
		}
	}
	
	function checkNewInputs(){
		if(!RDC.util.verifyText($("newContent").value, 1, 500)){
			PromptDialog.show("任务描述的字数在1到500之间");
			return false;
		}	
		if($("newTaskRank").selectedIndex == 0){
			PromptDialog.show("请选择任务的级别");
			return false;
		}
		if(!checkStartAndEndDates("newStartDate", "newStartTime", "newEndDate", "newEndTime")){
			return false;
		}
		var max = parseInt($("maxProportion").value);
		if(!RDC.util.verifyInteger($("newProportion").value, 1, max)){
			PromptDialog.show("占任务比例在1~" + max +"之间");
			return false;
		}
		return true;	
	}
	
	function checkStartAndEndDates(sd, st, ed, et){
		var val = $(sd).value + " " + $(st).value;
		var start = RDC.util.stringToDate(val, "yyyy-MM-dd HH:mm");
		if(start == 0){
			PromptDialog.show("预定开始日期或时间不合法");
			return false;
		}
		val = $(ed).value + " " + $(et).value;
		var end = RDC.util.stringToDate(val, "yyyy-MM-dd HH:mm");
		if(end == 0){
			PromptDialog.show("预定结束日期或时间不合法");
			return false;
		}
		if(start >= end){
			PromptDialog.show("结束时间不能早于开始时间");
			return false;
		}
		return true;
	}
	
	function createNewTask(){		
		if(!checkNewInputs())
			return;

		var task = {id:null};
		task.aspirantId = ssn_uid;
		task.goalId = selectedGoalId;
		task.content = $("newContent").value;
		task.scheduledStartDate = RDC.util.stringToDate($("newStartDate").value + " " + $("newStartTime").value, RDC.jsconst.DATE_FORMAT_yMdHm);
		task.scheduledEndDate = RDC.util.stringToDate($("newEndDate").value + " " + $("newEndTime").value, RDC.jsconst.DATE_FORMAT_yMdHm);
		task.proportionOfGoal = $("newProportion").value;
		task.taskRank = $("newTaskRank").selectedIndex;
		task.visibility = $("limitedAccess").selectedIndex;
		RDC.gui.disable("createTask");
		TaskHandler.saveTask(ssn_uid, task, function(result){
			if(result == null){
				PromptDialog.show("保存任务失败");
			}
			else {
				// switch to task view 
				viewTaskById(result.id);
				setCurTask(result.id);
			}
			RDC.gui.enable("createTask");
		});
	}
		
	function viewTaskById(tid){
		if(tid != null){
			var uid = (req_uid == "" || req_uid == null) ? ssn_uid : req_uid;
			window.location.href = "task.do?act=view&uid=" + uid + "&id=" + tid;
		}
	}
	
	function viewTaskFromEdit(){
		$("viewContent").innerHTML = $("editContent").value;
		$("viewStartDate").innerHTML = $("editStartDate").value + " " + $("editStartTime").value;
		$("viewEndDate").innerHTML = $("editEndDate").value + " " + $("editEndTime").value;
		$("viewProportion").innerHTML = $("editProportion").value;
		$("viewScore").innerHTML = $("editScore").value;
		$("viewRank").innerHTML = $("editTaskRank").value;
		$("viewVisibility").innerHTML = $("limitedAccess").value;
		showSection("view");
	}
	
	// -------------------------------------
	// save tasks
	// switch from View section
	function switchToEditSec(){
		$("editContent").value = $("viewContent").innerHTML;
		var st = $("viewStartDate").innerHTML.split(" ");
		$("editStartDate").value = st[0];
		$("editStartTime").value = st[1];
		var ed = $("viewEndDate").innerHTML.split(" ");
		$("editEndDate").value = ed[0];
		$("editEndTime").value = ed[1];
		$("editProportion").value = $("viewProportion").innerHTML;
		$("editTaskRank").value = $("viewRank").innerHTML;
		$("editScore").value = $("viewScore").innerHTML;
		$("editProportion").disabled = (selectedGoalId == null) ? true : false;
		$("limitedAccess").value = $("viewVisibility").innerHTML;
	}
	
	function checkEditInputs(){
		if(!RDC.util.verifyText($("editContent").value, 1, 500)){
			PromptDialog.show("任务描述的字数在1到500之间");
			return false;
		}	
		if($("editTaskRank").selectedIndex == 0){
			PromptDialog.show("请选择任务的级别");
			return false;
		}
		if(!checkStartAndEndDates("editStartDate", "editStartTime", "editEndDate", "editEndTime")){
			return false;
		}
		if(!RDC.util.verifyInteger($("editProportion").value, 1, 100)){
			PromptDialog.show("占任务比例在1~100之间");
			return false;
		}
		if(!RDC.util.verifyInteger($("editScore").value, 1, 100)){
			PromptDialog.show("自评分在1~100之间");
			return false;
		}
		return true;	
	}
	
	function saveTask(){
		if(checkEditInputs() == false)
			return;
			
		var tid = getCurTask();
		var task = {id:tid};
		task.aspirantId = ssn_uid;
		task.goalId = selectedGoalId;
		task.content = $("editContent").value;
		task.selfScore = $("editScore").value;
		task.taskRank = $("editTaskRank").selectedIndex;		
		task.status = $("viewStatus").value;
		task.proportionOfGoal = $("editProportion").value;
		task.visibility = $("limitedAccess").selectedIndex;
		var dt = $("realStartDate").innerHTML;
		if(dt != null && dt != ""){
			task.realStartDate = RDC.util.stringToDate(dt, RDC.jsconst.DATE_FORMAT_yMd);			
			dt = $("realEndDate").innerHTML;
			if(dt != null && dt != ""){
				task.realEndDate = RDC.util.stringToDate(dt, RDC.jsconst.DATE_FORMAT_yMd);
			}
		}
		task.scheduledStartDate = RDC.util.stringToDate($("editStartDate").value + " " + $("editStartTime").value, RDC.jsconst.DATE_FORMAT_yMdHm);
		task.scheduledEndDate = RDC.util.stringToDate($("editEndDate").value + " " + $("editEndTime").value, RDC.jsconst.DATE_FORMAT_yMdHm);
		task.createdTime = RDC.util.stringToDate($("viewCreated").innerHTML, RDC.jsconst.DATE_FORMAT_yMdHms);
		RDC.gui.disable("saveTask");
		TaskHandler.saveTask(ssn_uid, task, function(result){
			if(result == null){
				PromptDialog.show("保存任务失败");
			}
			else {
				if(getCurTask() == "" || getCurTask() == null){
					viewTaskById(result.id);
				}
				else {
					$("viewUpdated").innerHTML = RDC.util.dateToString(result.updatedTime, RDC.jsconst.DATE_FORMAT_yMdHms);
					viewTaskFromEdit();
				}
			}
			RDC.gui.enable("saveTask");
		});
	}
	
	// -------------------------------------------------
	function getTaskContent(){
		var val = $("viewContent").innerHTML;
		if(val.length > 10){
			val = val.substring(0, 10) + "...";
		}
		return "<b>" + val + "</b>";
	}
	
	// delete task		
	function deltTask(tid){
		var popupWin = new PopUpWindow("信息提示", "您确定要删除任务" + getTaskContent() + "吗？<br>删除任务同时也会删除任务的所有执行", 0);
		popupWin.addAction(deleteAction(popupWin, tid));
		popupWin.addAction(closeAction(popupWin));
		popupWin.show();
	}
	
	function deleteAction(dlg, tid){
		var btn = dlg.createAction("确定");
		btn.id = "confirm_delete";
		btn.onclick = function(){
			RDC.gui.disable(this.id);
			this.blur();				
			TaskHandler.deleteTask(ssn_uid, tid, function(result){
				if(result == false){
					dlg.updateMessage("删除任务失败，请稍后再试");
				}
				else {
					var uid = (req_uid == "" || req_uid == null) ? ssn_uid : req_uid;
					window.location.href = "<%=basePath%>task.do?act=all&uid=" + ssn_uid;					
				}				
			});		
		}
		return btn;
	}
	
	function closeAction(dlg){
		var btn = document.createElement("input");		
		btn.type = "button";
		btn.className = "cancelBtn";
		btn.value = "返回";
		btn.onclick = function(){
			dlg.dispose();
		};		
		return btn;
	}
	
	// -------------------------------------------------
	// more operations
	function createDropMenu(){
		if(!RDC.gui.exist("btnSubSec")){
			return;
		}
		
		dropMenu = new DropMenu("60px");
		var item = dropMenu.createItem("新建");
		item.id = "item_create";
		item.onclick = function(){
			window.location.href = "task.do?act=create";
		};
		dropMenu.addItem(item);
		
		item = dropMenu.createItem("完成");
		item.id = "item_complete";
		item.onclick = function(){
			var status = $("viewStatus").value;
			var going = "<%=BaseConst.LIFETASK_STATUS_UNDERGOING%>";
			if(status == going){
				completeTask(getCurTask());
			}
			else {
				PromptDialog.show("只有进行中的任务才能执行完成操作");
			}
			
			this.blur();
		};
		dropMenu.addItem(item);
				
		item = dropMenu.createItem("取消");
		item.id = "item_cancel";
		item.onclick = function(){
			var status = $("viewStatus").value;
			var cpt = "<%=BaseConst.LIFETASK_STATUS_COMPLETED%>";
			var cld = "<%=BaseConst.LIFETASK_STATUS_CANCELED%>";
			if(status != cpt && status != cld){
				cancelTask(getCurTask());
			}
			else {
				PromptDialog.show("已完成的和已取消的任务不能被取消");
			}
			
			this.blur();
		};
		dropMenu.addItem(item);
		
		item = dropMenu.createItem("删除");
		item.id = "item_delete";
		item.onclick = function(){
			deltTask(getCurTask());
			this.blur();
		};
		dropMenu.addItem(item);	
	}
	
	// complete the task
	function completeTask(tid){
		var popupWin = new PopUpWindow("信息提示", "您确定任务" + getTaskContent() + "已经完成？", 0);
		popupWin.addAction(completeAction(popupWin, tid));
		popupWin.addAction(closeAction(popupWin));			
		popupWin.show();
	}
	
	function completeAction(dlg, tid){
		var btn = dlg.createAction("确定");
		btn.id = "comfirm_complete";
		btn.onclick = function(){
			RDC.gui.disable(this.id);
			this.blur();
			TaskHandler.completeTask(ssn_uid, tid, function(result){
				if(result === null){
					dlg.updateMessage("完成操作失败，请稍后再试");					
				}
				else {
					dlg.updateMessage("恭喜您完成了一个任务，再接再厉");
					dlg.removeAction(btn);
					$("viewStatusDes").innerHTML = "<%=RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[BaseConst.LIFETASK_STATUS_COMPLETED] %>";
					$("viewStatus").value = "<%=BaseConst.LIFETASK_STATUS_COMPLETED%>";
					$("viewUpdated").innerHTML = RDC.util.dateToString(result.updatedTime, RDC.jsconst.DATE_FORMAT_yMdHms);
					$("realEndDate").innerHTML = RDC.util.dateToString(result.realEndDate, RDC.jsconst.DATE_FORMAT_yMd);
				}				
			});
		};
		return btn;
	}
	
	function cancelTask(tid){
		var popupWin = new PopUpWindow("信息提示", "您确定取消任务" + getTaskContent() + "？", 0);
		popupWin.addAction(cancelAction(popupWin, tid));
		popupWin.addAction(closeAction(popupWin));			
		popupWin.show();
	}
	
	function cancelAction(dlg, tid){
		var btn = dlg.createAction("确定");
		btn.id = "comfirm_cancel";
		btn.onclick = function(){
			RDC.gui.disable(this.id);
			this.blur();
			TaskHandler.cancelTask(ssn_uid, tid, function(result){
				if(result === null){
					dlg.updateMessage("取消任务失败，请稍后再试");					
				}
				else {
					dlg.updateMessage("取消任务成功");
					dlg.removeAction(btn);
					$("viewStatusDes").innerHTML = "<%=RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[BaseConst.LIFETASK_STATUS_CANCELED] %>";
					$("viewStatus").value = "<%=BaseConst.LIFETASK_STATUS_CANCELED%>";
					$("viewUpdated").innerHTML = RDC.util.dateToString(result.updatedTime, RDC.jsconst.DATE_FORMAT_yMdHms);
				}				
			});
		};
		return btn;
	}

</script>
</logic:present>

</body>

</html>
